package com.noble.meether.chat.msgtype;

import com.noble.library.utils.JsonUtil;
import com.noble.meether.chat.MessageConstant;
import com.noble.meether.chat.msgtype.base.BaseMessage;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 普通消息： 文本，图片，录制语音，小视频
 */
public class CommonMessage extends BaseMessage {
    // 文本消息
    private String text;            // 文本内容

    // 图片消息
    private String image;           // 图片地址
    private String localImage;      // 下载到本地图片地址

    // 小视频消息
    private String video;           // 视频地址
    private String localVideo;      // 下载到本地视频地址
    private String thumbPic;        // 视频缩略图
    private String localThumbPic;   // 下载到本地视频缩略图地址
    private String duration;        // 视频时长（暂时未处理）

    // 礼物消息
    private String giftIcon;
    private String giftName;
    private int giftNum;

    public CommonMessage() {
        super();
    }

    /**
     * 文本, 图片，小视频
     *
     * @param type 消息类型
     */
    public CommonMessage(int type, String content) {
        Map<String, Object> jsonMap = new HashMap<>();
        setMsgType(type);
        if (type == MessageConstant.MSG_ID_TEXT) {
            jsonMap.put("Text", content);
        } else if (type == MessageConstant.MSG_ID_PIC) {
            jsonMap.put("Image", content);
        }
        super.getJson(JsonUtil.mapToJSONString(jsonMap));
    }

    /**
     * 小视频
     *
     * @param video      视频地址
     * @param videoThumb 缩略图地址
     * @param duration   时长
     */
    public CommonMessage(String video, String localVideo, String videoThumb, String localThumb, String duration) {
        Map<String, Object> jsonMap = new HashMap<>();
        setMsgType(MessageConstant.MSG_ID_VIDEO);
        jsonMap.put("Video", video);
        jsonMap.put("LocalVideo", localVideo);
        jsonMap.put("Thumb", videoThumb);
        jsonMap.put("LocalThumb", localThumb);
        jsonMap.put("Duration", duration);
        super.getJson(JsonUtil.mapToJSONString(jsonMap));
    }

    /**
     * 礼物
     */
    public CommonMessage(String giftIcon, String giftName, int giftNum) {
        Map<String, Object> jsonMap = new HashMap<>();
        setMsgType(MessageConstant.MSG_ID_GIFT);
        jsonMap.put("Image", giftIcon);
        jsonMap.put("Text", giftName);
        jsonMap.put("Size", giftNum);
        super.getJson(JsonUtil.mapToJSONString(jsonMap));
    }

    @Override
    public BaseMessage parseJson(String jsonStr) {
        super.parseJson(jsonStr);
        parseContent(getContent());
        return this;
    }

    public void parseContent(String content) {
        JSONObject jsonObject = JsonUtil.getJsonObject(content);
        if (getMsgType() == MessageConstant.MSG_ID_TEXT) {  // 文字消息
            this.text = jsonObject.optString("Text");
        } else if (getMsgType() == MessageConstant.MSG_ID_PIC) {
            this.image = jsonObject.optString("Image");
            this.localImage = jsonObject.optString("LocalImage");   // 本地图片
        } else if (getMsgType() == MessageConstant.MSG_ID_VIDEO) {
            this.video = jsonObject.optString("Video");
            this.localVideo = jsonObject.optString("LocalVideo");
            this.thumbPic = jsonObject.optString("Thumb");
            this.localThumbPic = jsonObject.optString("LocalThumb");
            this.duration = jsonObject.optString("Duration");
        } else if (getMsgType() == MessageConstant.MSG_ID_GIFT) {
            this.giftName = jsonObject.optString("Text");
            this.giftIcon = jsonObject.optString("Image");
            this.giftNum = jsonObject.optInt("Size");
        }
    }

    /**
     * 解析礼物消息
     */
    public void parseGift(String content) {
        JSONObject jsonObject = JsonUtil.getJsonObject(content);
        this.giftName = jsonObject.optString("Text");
        this.giftIcon = jsonObject.optString("Image");
        this.giftNum = jsonObject.optInt("Size");
    }

    public String getGiftIcon() {
        return giftIcon;
    }

    public String getGiftName() {
        return giftName;
    }

    public int getGiftNum() {
        return giftNum;
    }

    public String getText() {
        return text;
    }

    public String getImage() {
        return image;
    }

    public String getVideo() {
        return video;
    }

    public String getThumbPic() {
        return thumbPic;
    }

    public String getDuration() {
        return duration;
    }

    public String getLocalImage() {
        return localImage;
    }

    public void setLocalImage(String localImage) {
        this.localImage = localImage;
    }

    public String getLocalVideo() {
        return localVideo;
    }

    public void setLocalVideo(String localVideo) {
        this.localVideo = localVideo;
    }

    public String getLocalThumbPic() {
        return localThumbPic;
    }

    public void setLocalThumbPic(String localThumbPic) {
        this.localThumbPic = localThumbPic;
    }
}
